const connection = require('./connect');
const fs = require('fs');

//查询


function query() {
    return new Promise(function (resolve, reject) {
        connection.query('SELECT * from jijin_list', function (error, results) {
            if (error) {
                reject(error);
                return;
            }
            resolve(results)
        });
    })

}

var logger_success = fs.createWriteStream('./log/log_success' + new Date().toLocaleDateString() + '.txt', {
    flags: 'a' // 'a' means appending (old data will be preserved)
});
var logger_error = fs.createWriteStream('./log/log_error' + new Date().toLocaleDateString() + '.txt', {
    flags: 'a' // 'a' means appending (old data will be preserved)
});

async function update(data, index, callback) {
    var addSql = 'INSERT INTO jijin_list (id,name,abc_name,val_1,val_sum_1,val_2,val_sum_2,day_add_num,day_add_rate,shengou,shuhui) VALUES (?,?,?,?,?,?,?,?,?,?,?)';
    await connection.query(addSql, data, function (err, rows) {
        if (err) {
            console.log('insert failure  第' + index + '条');
            logger_error.write(new Date().toLocaleDateString() + '' + JSON.stringify(rows) + '\n' + JSON.stringify(err) + '\n');
            callback && callback(err);
            return;
        }
        logger_success.write(new Date().toLocaleDateString() + '' + JSON.stringify(rows) + '\n');
        console.log('insert success  第' + index + '条');
        callback && callback('success')
    });
}

async function insertJJDetail(data, index, callback) {
    var insertSql = "INSERT INTO jj_detail (number,jj_gp_id,gp_id,gp_name,jj_rate,jj_gp_amount,jj_gp_money,jj_id,quarter,year) VALUES (?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE number=number,jj_gp_id=jj_gp_id,gp_id=gp_id,gp_name=gp_name,jj_rate=jj_rate,jj_gp_amount=jj_gp_amount,jj_gp_money=jj_gp_money,jj_id=jj_id,quarter=quarter,year=year";
    // var insertSql = "INSERT INTO jj_detail (number,jj_gp_id,gp_id,gp_name,jj_rate,jj_gp_amount,jj_gp_money,jj_id,quarter,year) VALUES (?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE number='$number',jj_gp_id='$jj_gp_id',gp_id='$gp_id',gp_name='$gp_name',jj_rate='$jj_rate',jj_gp_amount='$jj_gp_amount',jj_gp_money='$jj_gp_money',jj_id='$jj_id',quarter='$quarter',year='$year'";
    // var insertSql = "INSERT INTO jj_detail (number,jj_gp_id,gp_id,gp_name,jj_rate,jj_gp_amount,jj_gp_money,jj_id,quarter,year) VALUES (?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE jj_gp_id=jj_gp_id";
    // var insertSql = "INSERT INTO jj_detail (number,jj_gp_id,gp_id,gp_name,jj_rate,jj_gp_amount,jj_gp_money,jj_id) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE jj_gp_id='$jj_gp_id'";
    await connection.query(insertSql, data, function (err, rows) {
        if (err) {
            // console.log('insertJJDetail failure  第' + index + '条');
            // logger_error.write(new Date().toLocaleDateString() + '' + JSON.stringify(rows) + '\n' + JSON.stringify(err) + '\n');
            callback && callback(err);
            return;
        }
        // logger_success.write(new Date().toLocaleDateString() + '' + JSON.stringify(rows) + '\n');
        // console.log('insertJJDetail success  第' + index + '条');
        callback && callback('insertJJDetail success ' + index)
    });
}


const sql = {
    query,
    update,
    insertJJDetail
}


module.exports = sql;
